package com.timerecording.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.timerecording.common.SwaggerTagConst;
import com.timerecording.common.controller.RecordTimeAdminBaseController;
import com.timerecording.domain.entity.OperateLogEntity;
import com.timerecording.domain.entity.ReSalaryEntity;
import com.timerecording.domain.entity.ReTeamEntity;
import com.timerecording.domain.entity.RecordTimeEntity;
import com.timerecording.domain.form.ReSalaryForm;
import com.timerecording.domain.form.RecordTimeAddForm;
import com.timerecording.domain.vo.RecordTimeVO;
import com.timerecording.service.RecordTimeService;
import com.timerecording.util.R;
import com.timerecording.util.ResponseDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @author smz41
 * @description: TODO
 * @date 2024/4/4
 */

@CrossOrigin  //解决跨域
@RestController
@Api(tags = {SwaggerTagConst.Admin.RECORDING_TIME_SYSTEM})
public class RecordTimeController extends RecordTimeAdminBaseController {


    @Autowired
    private RecordTimeService recordTimeService;


    /** 新增工时记录
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */

    @ApiOperation("新增工时记录 @author smz")
    @PostMapping("/insOrupdRecordTime")
    public R insOrupdRecordTime(@ApiParam("添加工时记录") @RequestBody @Valid RecordTimeAddForm recordAddForm) throws UnknownHostException {
        return recordTimeService.insOrupdRecordTime(recordAddForm);
    }


    /** 两表查询小时工资
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */
    @ApiOperation("查询当前主表小时工资 @author smz")
    @GetMapping("/queryHourSalary")
    public ResponseDTO<List<ReSalaryEntity>> queryHourSalary(){
        return recordTimeService.queryHourSalary();
    }


    /** 两表查询班次名称
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/4
     */

    @ApiOperation("查询当前主表班次 @author smz")
    @GetMapping("/queryTeam")
    public ResponseDTO<List<ReTeamEntity>> queryTeam(){
        return recordTimeService.queryTeam();
    }


    /** 查询工时列表    可以复用的方法
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/5
     */

    @ApiOperation("查询工时列表 @author smz")
    @GetMapping("/queryRecordTimeList")
    public ResponseDTO<List<RecordTimeVO>> queryRecordTimeList(@ApiParam(value = "条件查询时间") String recordTime){
        return recordTimeService.queryRecordTimeList(recordTime);
    }

    /** 删除工时记录
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/9
     */

    @ApiOperation("删除工时记录 @author smz")
    @GetMapping("/deleteRecordTime/{recordId}")
    public R deleteRecordTime(@ApiParam(value = "工时表主键id") @PathVariable("recordId") Integer recordId) throws UnknownHostException {
        return recordTimeService.deleteRecordTime(recordId);
    }


    /** 回显工时信息  用于编辑工时
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/4/20
     */

    @ApiOperation("编辑工时  @author smz")
    @GetMapping("/eduitRecordTime/{recordId}")
    public ResponseDTO<List<RecordTimeVO>> eduitRecordTime(@ApiParam(value = "工时主表id") @PathVariable("recordId") Integer recordId){
        return recordTimeService.eduitRecordTime(recordId);
    }


    /**  这个方法没用
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/1
     */

    @ApiOperation("查询本月工作天数 @author smz")
    @GetMapping("/workDayQuery")
    public int workDayQuery(@ApiParam(value = "工时时间") String recordTime){
        return recordTimeService.workDayQuery(recordTime);
    }





    /** 小时工资查询
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/1
     */

    @ApiOperation("小时工资表查询 @author smz")
    @GetMapping("/queryWorkHour")
    public ResponseDTO<List<ReSalaryEntity>> queryWorkHour(){
        return recordTimeService.queryWorkHour();
    }


    /** 小时工资删除  此为逻辑删除不是物理删除
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    @ApiOperation("小时工资逻辑删除 @author smz")
    @GetMapping("/deleteWorkHourById/{salaryId}")
    public R deleteWorkHourById(@ApiParam(value = "小时工资表主键id") @PathVariable("salaryId") Integer salaryId) throws UnknownHostException {
        return recordTimeService.deleteWorkHourById(salaryId);
    }


    /** 小时工资恢复状态delete_flag = 0
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/7/15
     */

    @ApiOperation("小时工资恢复 @author smz")
    @PostMapping("/recoverSalaryById/{salaryId}")
    public R recoverSalaryById(@ApiParam(value = "小时工资表主键id") @PathVariable("salaryId") Integer salaryId) throws UnknownHostException {
        return recordTimeService.recoverSalaryById(salaryId);
    }


    /** 小时工资表单表新增或者修改
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    @ApiOperation("小时工资表新增或修改 @author smz")
    @PostMapping("/insOrupdWorkHour")
    public R insOrupdWorkHour(@ApiParam(value = "新增或修改小时工资表单表") @RequestBody @Valid ReSalaryForm reSalaryForm) throws UnknownHostException {
        return recordTimeService.insOrupdWorkHour(reSalaryForm);
    }

    /** 小时工资每条记录编辑详情
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    @ApiOperation("小时工资表每条记录的详情 @author smz")
    @GetMapping("/eduitWorkHour/{salaryId}")
    public ResponseDTO<ReSalaryEntity> eduitWorkHour(@ApiParam(value = "工资表主键id") @PathVariable Integer salaryId){
        return recordTimeService.eduitWorkHour(salaryId);
    }

    /** 日志记录查询
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    @ApiOperation("日志记录表查询 @author smz")
    @GetMapping("/queryLog")
    public ResponseDTO<List<OperateLogEntity>> queryLog(){
        return recordTimeService.queryLog();
    }


    /** 导出工时列表
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/5/2
     */

    @ApiOperation("导出工时记录 @author smz")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //获取数据库的数据
        List<RecordTimeEntity>  list = recordTimeService.exportExcel();
        //定义文件名称
        String fileName = "recordTime.xlsx";

        //创建ExcelWriter
        ExcelWriter writer = ExcelUtil.getWriter();

        writer.addHeaderAlias("recordId","ID");
        writer.addHeaderAlias("recordTime","当前日期");
        writer.addHeaderAlias("workHour","工作时长");
        writer.addHeaderAlias("remark","备注");
        writer.addHeaderAlias("createTime","创建时间");
        writer.addHeaderAlias("salaryMoney","小时工资数");
        writer.addHeaderAlias("teamName","班次名称");
        //将查询的字段写入到对应的ExcelWriter
        writer.write(list);

        //设置response的Header
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition","attachment; fileName"+fileName);


        //写出Excel到response
        writer.flush(response.getOutputStream(),true);
        writer.close();

    }




    //以下是新的工时记录的接口



}
